﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Entities;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using GameEngine.Camera;

namespace GameEngine.Scenes
{
    public class Layer2D
    {
        #region Fields

        private Texture2D background;
        private List<AbstractEntity> sceneObjects = new List<AbstractEntity>();
        private Game game;

        #endregion
        #region Properties

        /// <summary>
        /// Gets or sets the background.
        /// </summary>
        /// <value>
        /// The background.
        /// </value>
        public Texture2D Background
        {
            get { return background; }
            set { background = value; }
        }

        /// <summary>
        /// Gets or sets the scene objects.
        /// </summary>
        /// <value>
        /// The scene objects.
        /// </value>
        public List<AbstractEntity> SceneObjects
        {
            get { return sceneObjects; }
            set { sceneObjects = value; }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Layer2D" /> class.
        /// </summary>
        /// <param name="game">The game.</param>
        public Layer2D(Game game)
        {
            this.game = game;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Layer2D" /> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="sceneObjects">The scene objects.</param>
        public Layer2D(Game game, List<AbstractEntity> sceneObjects)
        {
            this.game = game;
            this.sceneObjects = sceneObjects;
        }

        /// <summary>
        /// Gets the scene objects.
        /// </summary>
        /// <returns></returns>
        public List<AbstractEntity> GetSceneObjects()
        {
            return sceneObjects;
        }

        /// <summary>
        /// Draws this layer
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        /// <param name="backgroundRectangle">The background rectangle.</param>
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime, Rectangle backgroundRectangle)
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null,
                        ((Camera2D)game.Services.GetService(typeof(Camera2D))).GetTransformation());

            if (background != null)
                spriteBatch.Draw(background, backgroundRectangle, Color.White);

            spriteBatch.End();
        }

    }
}
